package com.meihua.shellapp.activity.maker;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.PolicyManageAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.databean.SupplierDataBean;
import com.meihua.shellapp.dialog.PolicyManageScreenDialog;
import com.meihua.shellapp.fragment.policy.PolicyManageFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/27.
 * 邮箱：lc0607live@163.com
 */

public class PolicyManageActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题
    @Bind(R.id.iv_top_right)
    ImageView iv_top_right;//筛选

    @Bind(R.id.tl_policymanage)
    TabLayout tl_policymanage;//头部导航
    @Bind(R.id.vp_policymanage)
    ViewPager vp_policymanage;//头部导航


    private List<Fragment> mFragment = new ArrayList<>();
    private PolicyManageAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_policymanage;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tv_title.setText("保单管理");
        iv_top_left.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        iv_top_right.setVisibility(View.VISIBLE);
        iv_top_right.setOnClickListener(this);
        List<String> type = new ArrayList<>();
        type.add("首期");
        type.add("续期");
        type.add("应收清单");
        mFragment.add(new PolicyManageFragment().setType(1));
        mFragment.add(new PolicyManageFragment().setType(2));
        mFragment.add(new PolicyManageFragment().setType(3));
        mAdapter = new PolicyManageAdapter(getSupportFragmentManager(), type, mFragment);
        vp_policymanage.setAdapter(mAdapter);
        tl_policymanage.setupWithViewPager(vp_policymanage);
        LinearLayout linearLayout = (LinearLayout) tl_policymanage.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.bg_parting_ordertl));
        linearLayout.setDividerPadding((int) (getWH()[0] * (22 / 750.0)));
        iv_top_left.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_top_right://筛选
                final PolicyManageScreenDialog dialog = new PolicyManageScreenDialog(mContext) {
                    @Override
                    public void forResult(String startTimeStr, String endTimeStr, String nameStr, int typeIndex, int statusIndex, SupplierDataBean supplierBean) {
                        setScreenCondition(startTimeStr, endTimeStr, nameStr, typeIndex, statusIndex, supplierBean);
                    }
                };
                dialog.show();
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = (int) (getWH()[0] * (560.0 / 750));//设置宽度
                lp.gravity = Gravity.RIGHT;
                dialog.getWindow().setAttributes(lp);
                break;
        }
    }

    /**
     * 设置筛选条件
     *
     * @param startTimeStr 开始时间
     * @param endTimeStr   结束时间
     * @param nameStr      被保人姓名
     * @param typeIndex    保单类型 1， 2，3
     * @param statusIndex  保单状态  1，2
     * @param supplierBean 供应商
     */
    private void setScreenCondition(String startTimeStr, String endTimeStr, String nameStr, int typeIndex, int statusIndex, SupplierDataBean supplierBean) {
        typeIndex++;
        statusIndex++;
        for (int i = 0; i < mFragment.size(); i++) {
            ((PolicyManageFragment) mFragment.get(i)).setScreenCondition(startTimeStr, endTimeStr, nameStr, typeIndex, statusIndex, supplierBean);
        }
    }
}
